import QtQuick 2.15

Item {
    id: root

    property int threshold: 10        // 滑动阈值(px)
    property alias timeoutValue: tapHandler.longPressThreshold// 长按阈值(ms)
    property alias point:tapHandler.point

    signal swipe(string direction, int value)   // 上、下、左、右
    signal clicked(string keyStr)               // short / long / left / right / up / down

    // 点击、长按处理
    TapHandler {
        id: tapHandler
        longPressThreshold: 0.5   // 长按时间阈值

        onTapped: root.clicked("short")
        onLongPressed: root.clicked("long")
    }

    // 滑动/拖动处理
    DragHandler {
        id: dragHandler
        // 鼠标松开时才判断方向
        onActiveChanged: {
            if (!active) {
                if (Math.abs(translation.x) > Math.abs(translation.y)) {
                    let dir = translation.x < 0 ? "left" : "right"
                    root.swipe(dir, Math.abs(translation.x))
                    root.clicked(dir)
                } else if (Math.abs(translation.y) > root.threshold) {
                    let dir = translation.y < 0 ? "up" : "down"
                    root.swipe(dir, Math.abs(translation.y))
                    root.clicked(dir)
                }
            }
        }
    }
}
